export default {
    namespaced: true,
    state: {
        // 购物车数组
        cartList: uni.getStorageSync('cartList') || []
    },
    getters: {
        count(state) {
            // 累加所有的num
            let result = 0
            state.cartList.forEach(goods => {
                result += goods.num
            })
            return result
        },
        // 过滤有勾选的数组
        checkedList(state) {
            return state.cartList.filter(item => item.checked)
        },
        // 全选状态
        isCheckAll(state) {
            // 如果全选了，全选框的状态也改为true
            // 当数组为空时也返回true  短路运算
            return state.cartList.length && state.cartList.every(item => item.checked)
        },
        // 合计
        addCount(state, getters) {
            console.log(getters);
            // checked为true的才要选中
            let count = 0
            getters.checkedList.forEach(item => {
                count += item.goods_price * item.num
            })
            return count
        },
        // 合计数量
        addNum(state, getters) {
            // checked为true的才要选中
            let count = 0
            getters.checkedList.forEach(item => {
                count += item.num
            })
            return count
        }
    },
    mutations: {
        // 判断数组中id与现在点击的id是否一致，如果一致就(要获取当前的对象，通过index)数量+1  如果不存在就push进数组
        // payload以对象的形式传入{value：数据对象}--方便后面的维护
        addList(state, payload) {
            const index = state.cartList.findIndex(item => item.goods_id === payload.value.goods_id)
            //  不存在返回-1
            if (index === -1) {
                state.cartList.push({ ...payload.value, num: 1, checked: true })
            } else {
                state.cartList[index].num++
            }
            uni.showToast({
                title: '添加成功',
                icon: ''
            })
            console.log('state.cartList', state.cartList);
            // 本地存储
            uni.setStorageSync('cartList', state.cartList)
        },
        // 改变多选框状态
        changeCheckbox(state, payload) {
            const index = state.cartList.findIndex(item => {
                return item.goods_id === payload.value
            })
            state.cartList[index].checked = !state.cartList[index].checked
            // 只要对数据进行处理，都要放进缓存
            uni.setStorageSync('cartList', state.cartList)
        },
        // 主动全选
        changeCheckAll(state, payload) {
            const isCheckAll = !payload.value
            state.cartList.forEach(item => {
                item.checked = isCheckAll
            })
            uni.setStorageSync('cartList', state.cartList)
        },
        // 数量的增减
        async changeCartNum(state, payload) {
            // 找到当前点击的商品
            const index = state.cartList.findIndex(item => {
                return item.goods_id === payload.value.goods_id
            })
            // 当点击为-1和数量=0时，弹出确认框是否删除
            if (payload.value.num === -1 && state.cartList[index].num === 1) {
                const [err, res] = await uni.showModal({
                    title: '确认删除',
                    content: '确认删除该商品吗?',
                    showCancel: true,
                    cancelText: '取消',
                    cancelColor: '#000000',
                    confirmText: '确定',
                    confirmColor: '#3CC51F',
                });
                if (err) {
                    console.log(err);
                    uni.showToast('删除错误')
                    return
                }
                if (res.confirm) {
                    // 确认删除，删除数组的数据
                    state.cartList.splice(index, 1)
                }
            }
            // 如果不是直接进行数据的加减
            else {
                // 改变数组当前的num数量
                state.cartList[index].num += payload.value.num
            }

            uni.setStorageSync('cartList', state.cartList)
        }
    },
    actions: {

    }
}